package app;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.atguigu.edu.realtime.common.base.BaseApp;
import com.atguigu.edu.realtime.common.bean.UserLoginBean;
import com.atguigu.edu.realtime.common.constant.Constant;
import com.atguigu.edu.realtime.common.function.BeanToJsonStrMapFunction;
import com.atguigu.edu.realtime.common.util.DateFormatUtil;
import com.atguigu.edu.realtime.common.util.FlinkSinkUtil;
import org.apache.flink.api.common.eventtime.SerializableTimestampAssigner;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.api.common.functions.FilterFunction;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.common.functions.ReduceFunction;
import org.apache.flink.api.common.state.ValueState;
import org.apache.flink.api.common.state.ValueStateDescriptor;
import org.apache.flink.api.java.functions.KeySelector;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.AllWindowedStream;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.KeyedProcessFunction;
import org.apache.flink.streaming.api.functions.ProcessFunction;
import org.apache.flink.streaming.api.functions.windowing.AllWindowFunction;
import org.apache.flink.streaming.api.functions.windowing.ProcessAllWindowFunction;
import org.apache.flink.streaming.api.windowing.assigners.TumblingEventTimeWindows;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.streaming.api.windowing.windows.TimeWindow;
import org.apache.flink.util.Collector;

import java.time.Duration;

public class DwsUserUserLoginWindow extends BaseApp {
    public static void main(String[] args) {
        new DwsUserUserLoginWindow().start(
                10024,
                4,
                "dws_user_user_login_window",
                Constant.TOPIC_DWD_TRAFFIC_PAGE
        );
    }
    @Override
    public void handle(StreamExecutionEnvironment env, DataStreamSource<String> kafkaStrDS) {
        //1）读取 Kafka 页面主题数据
        // 2) 转换数据结构
        SingleOutputStreamOperator<JSONObject> JSONObjDS = kafkaStrDS.map(
                new MapFunction<String, JSONObject>() {
                    @Override
                    public JSONObject map(String value) throws Exception {
                        return JSON.parseObject(value);
                    }
                }
        );
       //3）过滤数据
        SingleOutputStreamOperator<JSONObject> filteredDS = JSONObjDS.filter(
                new FilterFunction<JSONObject>() {
                    @Override
                    public boolean filter(JSONObject value) throws Exception {
                        JSONObject commonJsonObj = value.getJSONObject("common");
                        JSONObject pageJsonObj = value.getJSONObject("page");
                        return (commonJsonObj.getString("uid")!=null)&&
                                (pageJsonObj.getString("last_page_id")==null);
                    }
                }
        );
        //filteredDS.print();
        //4）设置水位线
        SingleOutputStreamOperator<JSONObject> withWatermarksDS = filteredDS.assignTimestampsAndWatermarks(
                WatermarkStrategy.<JSONObject>forBoundedOutOfOrderness(Duration.ZERO)
                        .withTimestampAssigner(
                                new SerializableTimestampAssigner<JSONObject>() {
                                    @Override
                                    public long extractTimestamp(JSONObject element, long recordTimestamp) {
                                        return element.getLong("ts");
                                    }
                                }
                        )
        );
        //5）按照 uid 分组
        KeyedStream<JSONObject, String> keyedStream = withWatermarksDS.keyBy(
                new KeySelector<JSONObject, String>() {
                    @Override
                    public String getKey(JSONObject value) throws Exception {
                        return value.getJSONObject("common").getString("uid");
                    }
                }
        );
        //6）统计回流用户数
        SingleOutputStreamOperator<UserLoginBean> backCtDS = keyedStream.process(
                new KeyedProcessFunction<String, JSONObject, UserLoginBean>() {
                    private ValueState<String> valueState;

                    @Override
                    public void open(Configuration parameters) throws Exception {
                        ValueStateDescriptor<String> valueStateDescriptor = new ValueStateDescriptor<String>("valueStateDescriptor", String.class);
                        valueState = getRuntimeContext().getState(valueStateDescriptor);
                    }

                    @Override
                    public void processElement(JSONObject value, KeyedProcessFunction<String, JSONObject, UserLoginBean>.Context ctx, Collector<UserLoginBean> out) throws Exception {
                        String lastLoginDateState = valueState.value();
                        Long ts = value.getLong("ts");
                        String curdate = DateFormatUtil.tsToDate(ts);
                        Long uucnt = 0L;
                        Long backcnt = 0L;
                        if (lastLoginDateState != null) {
                            //if (!lastLoginDateState.equals(curdate)) {
                            if (lastLoginDateState.equals(curdate)) {
                                uucnt=1L;
                                backcnt=1L;
/*                                long days = (ts - DateFormatUtil.dateToTs(lastLoginDateState)) / 1000 / 60 / 60 / 24;
                                if (days >= 7) {
                                    backcnt=1L;
                                }*/
                            }
                        }else {
                            uucnt=1L;
                        }
                        if(uucnt==1||backcnt==1){
                            out.collect(
                                    new UserLoginBean(
                                            "",
                                            "",
                                            "",
                                            backcnt,
                                            uucnt,
                                            ts
                                    )
                            );
                            valueState.update(curdate);
                        }
                    }
                }
        );

        //backCtDS.print();

        AllWindowedStream<UserLoginBean, TimeWindow> allWindowedStream = backCtDS.windowAll(TumblingEventTimeWindows.of(Time.seconds(10)));
        SingleOutputStreamOperator<UserLoginBean> reducedDS = allWindowedStream.reduce(
                new ReduceFunction<UserLoginBean>() {
                    @Override
                    public UserLoginBean reduce(UserLoginBean value1, UserLoginBean value2) throws Exception {
                        value1.setBackCt(value1.getBackCt() + value2.getBackCt());
                        value1.setUuCt(value1.getUuCt() + value2.getUuCt());
                        return value1;
                    }
                },
                new AllWindowFunction<UserLoginBean, UserLoginBean, TimeWindow>() {
                    @Override
                    public void apply(TimeWindow window, Iterable<UserLoginBean> values, Collector<UserLoginBean> out) throws Exception {
                        long start = window.getStart();
                        long end = window.getEnd();
                        UserLoginBean userLoginBean = values.iterator().next();
                        userLoginBean.setStt(DateFormatUtil.tsToDateTime(start));
                        userLoginBean.setEdt(DateFormatUtil.tsToDateTime(end));
                        userLoginBean.setCurDate(DateFormatUtil.tsToDate(start));

                        out.collect(userLoginBean);
                    }
                }
        );

        reducedDS.print();
        SingleOutputStreamOperator<String> resultDS = reducedDS.map(new BeanToJsonStrMapFunction<>());
        resultDS.sinkTo(FlinkSinkUtil.getDorisSink("dws_user_user_login_window"));


    }
}
